package com.wdj.library.service.impl;

import com.wdj.library.dao.BookInfoDao;
import com.wdj.library.dao.MUserBookDao;
import com.wdj.library.entity.BookInfo;
import com.wdj.library.entity.MUserBook;
import com.wdj.library.entity.vo.Result;
import com.wdj.library.service.MUserBookService;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * (MUserBook)表服务实现类
 *
 * @author wdj
 * @since 2020-08-12 11:54:07
 */
@Service("mUserBookService")
public class MUserBookServiceImpl implements MUserBookService {

    @Resource
    private MUserBookDao mUserBookDao;

    @Resource
    private BookInfoDao bookInfoDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public MUserBook queryById(Integer id) {
        return this.mUserBookDao.queryById(id);
    }
    /**
     * @Author wdj
     * @Description //TODO 借阅书籍
     * @Date 17:06 2020/8/14
     * @Param [userId, bookId]
     * @return com.wdj.library.entity.vo.Result
     **/
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result borrowBook(Integer userId, Integer bookId) {
        BookInfo bookInfo = bookInfoDao.queryById(bookId);
        System.out.println(bookInfo);
        if (bookInfo.getRemainNum() <= 0) {
            return Result.error("无可借的书籍");
        }
        int num0 = bookInfoDao.updateSubRemain(bookId);
        if (num0 == 1){
            MUserBook mUserBook = new MUserBook();
            mUserBook.setUserId(userId);
            mUserBook.setBookId(bookId);
            mUserBook.setEffectiveTime(DateUtils.addDays(new Date(),30));
            mUserBook.setCreateTime(new Date());
            mUserBook.setUpdateTime(new Date());
            mUserBook.setState("借出");
            int num =  mUserBookDao.insert(mUserBook);
            if(num == 1){
                return Result.success("借阅成功");
            }else{
                return Result.error("借阅失败");
            }
        }else{
            return Result.error("借阅失败");
        }

    }
    /**
     * @Author wdj
     * @Description //TODO 归还书籍
     * @Date 17:05 2020/8/14
     * @Param [id]
     * @return com.wdj.library.entity.vo.Result
     **/
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result giveBack(Integer id) {
        int num = mUserBookDao.updateState(id);
        if (num == 0) {
            return Result.error("归还失败");
        }else {
            MUserBook mUserBook = mUserBookDao.queryById(id);
            int num1 = bookInfoDao.updateAddRemain(mUserBook.getBookId());
            if (num1 == 1){
                return Result.success("归还成功");
            }else{
                return Result.error("归还失败");
            }
        }
    }

    /**
     * @Author wdj
     * @Description //TODO 列出借阅列表
     * @Date 16:22 2020/8/14
     * @Param [userId]
     * @return com.wdj.library.entity.vo.Result
     **/
    @Override
    public Result borrowList(Integer userId) {
        List<MUserBook> mUserBooks = mUserBookDao.queryByUserId(userId);
        return Result.success(mUserBooks);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public Result queryAllByLimit(int offset, int limit) {
        return Result.success(this.mUserBookDao.queryAllByLimit(offset, limit));
    }

    /**
     * 新增数据
     *
     * @param mUserBook 实例对象
     * @return 实例对象
     */
    @Override
    public MUserBook insert(MUserBook mUserBook) {
        this.mUserBookDao.insert(mUserBook);
        return mUserBook;
    }

    /**
     * 修改数据
     *
     * @param mUserBook 实例对象
     * @return 实例对象
     */
    @Override
    public MUserBook update(MUserBook mUserBook) {
        this.mUserBookDao.update(mUserBook);
        return this.queryById(mUserBook.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.mUserBookDao.deleteById(id) > 0;
    }
}